package javagame;

import java.awt.event.*;
import java.util.*;

public class Input implements KeyListener, MouseListener, MouseMotionListener {
    private boolean[] pressed;
    private boolean[] down;
    private boolean[] released;
    
    private LinkedList<Integer> flags;
    
    private int mousex;
    private int mousey;
    private boolean mouse_down;
    
    private static final int SIZE = 525;
    
    public Input() {
        flags = new LinkedList<Integer>();
        pressed = new boolean[SIZE];
        down = new boolean[SIZE];
        released = new boolean[SIZE];
        for(int i = 0; i < SIZE; i++) {
            pressed[i] = false;
            down[i] = false;
            released[i] = false;
        }
        mousex = 0;
        mousey = 0;
        mouse_down = false;
    }
    
    // The key was just pushed down.
    public boolean isPressed(int i) {
        if(i < SIZE && i >= 0) {
            return pressed[i];
        }
        return false;
    }
    
    // The key is down.
    public boolean isDown(int i) {
        if(i < SIZE && i >= 0) {
            return down[i];
        }
        return false;
    }
    
    // The key was just released.
    public boolean isReleased(int i) {
        if(i < SIZE && i >= 0) {
            return released[i];
        }
        return false;
    }
    
    public void reset() {
        int f;
        while(flags.size() > 0) {
            f = flags.remove();
            pressed[f] = false;
            released[f] = false;
        }
    }
    
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if(code < SIZE) {
            pressed[code] = true;
            down[code] = true;
            flags.add(new Integer(code));
        }
    }
    
    public void keyReleased(KeyEvent e) {
        int code = e.getKeyCode();
        if(code < SIZE) {
            released[code] = true;
            down[code] = false;
            flags.add(new Integer(code));
        }
    }
    
    public void keyTyped(KeyEvent e) {
        // Implemented to satisfy KeyListener interface.
    }
    
    public void mouseMoved(MouseEvent e) {
        System.out.println("mouse moved to" + e.getX() + ", " + e.getY());
        mousex = e.getX();
        mousey = e.getY();
    }
    
    public void mouseDragged(MouseEvent e) {
        System.out.println("mouse dragged to" + e.getX() + ", " + e.getY());
        mousex = e.getX();
        mousey = e.getY();
    }
    
    public void mouseExited(MouseEvent e) {
        
    }
    
    public void mouseEntered(MouseEvent e) {
        
    }
    
    public void mouseClicked(MouseEvent e) {
        
    }
    
    public void mousePressed(MouseEvent e) {
        mouse_down = true;
    }
    
    public void mouseReleased(MouseEvent e) {
        mouse_down = false;
    }
    
    public int getMouseX() {
        return mousex;
    }
    
    public int getMouseY() {
        return mousey;
    }
    
    public boolean isMousePressed() {
        return mouse_down;
    }
}
